﻿
using System;

namespace parser
{
	/// <summary>
	/// Description of Evaluator.
	/// </summary>
	public class Evaluator
	{
		
		public static decimal Evaluate(TreeNode tn)
		{
			if (tn.lx.lt == LexemType.VALUE)
				return Convert.ToDecimal(tn.lx.val);
			else if(tn.lx.lt == LexemType.OPER)
			{
				switch(tn.lx.val)
				{
					case "+":
						return Evaluate(tn.left) + Evaluate(tn.right);

					case "-":
						return Evaluate(tn.left) - Evaluate(tn.right);

					case "*":
						return Evaluate(tn.left) * Evaluate(tn.right);

					case "/":
						return Evaluate(tn.left) / Evaluate(tn.right);
	
						
				}
			
			
			}
			
			return 0;
		
		}
		
		
		
		public Evaluator()
		{
		}
	}
}
